import key from 'keymaster';

import * as TemplatesService from '../../services/Editor/templates';

export default {
    namespace: 'editor',
    state: {
        preview: {
            showPreview: false,
            singleWidget: false,
            singleTimeline: false,
        },
        status: {
            data: {},
            displayDialog: '',
            isEdit: false,
            showAnimation: false,
            switchType: 'PlaneAR', // 编辑器类型：PlaneAR, H5AR, 3d
            showFloatingHead: false,
            open_permission_tip: false, // 无权限提示
            step: 1, // 帮助引导步骤
            showHelpMenu: false, // 显示帮助引导浮层
            permissionCode: null, // 存权当前权限 码
        },
        fromTemplate: false,
        scale: 1,
        centerTop: 0,
        centerLeft: 0,
        editorType: is.options.editor_type,
        refLines: {},
        page: null,
        focusTab: 'page',
        selectedTimeline: {},
        selectedWidget: {},
        overFixedBtn: false,
        addFixedBtnActionType: -1,
        is_logo_md5: false,
        is_floating_screen: false,
        is_h5_shared_url: false,
        is_picture_shared_url: false,
        showStandardBtns: false,
        is_page_back: false,
        showList: true, // 时间轴参数
        showTimeline: false, // 时间轴参数
        is_play_video: false, // 标记模板AR下替换播放视频
        showFormBtns: false, // 标记表单组件默认类型是否显示
        has_hopeBank: 0, // 希望金币
        has_lookers: 0, // 围观组件,
        has_red_packet: 0,
        page_timelines: [],
        widgetsCopy: [],
        editMode: null,
        dragging: false,
        play_music: false,
        disableCanvasSelectable: false,
        is_only_red: true,
        is_edit_video: false,
        is_edit_music: false,
        is_edit_u3d: false,
        is_edit_album: false,
        turning_antype: null,
        widgetAllVisible: true,
        toggleAllVisible: true,
        toggleAllLineVisible: true,
        selectedPageId: null,
        selectedWidgetIds: [],
        selectedAnType: null,
        selectedWidgetId: null,
        selectedAnTypeId: null,
        selectedTimelineId: null,
        selectedWidgetTimelineId: null,
        open_permission_tip: false,
        max_time: 0,
        max_end_time: 0,
        i18n_base_path: window.i18n_base_path || '',
        preloadImg: window.preloadImg || '',
        lan: window.lan,
        stillAnimation: false, // h5动效播放标志
        shiftKey: '', // 标记是否按下shift键
        rulerLeft: '', // 标尺位置
        menuType: 'editorMenus' //鼠标选项
    },
    reducers: {
        switchType(state, { payload: switchType }) {
            const status = state.status;
            status.switchType = switchType;
            return { ...state, status: { ...state.status, ...status } };
        },
        setState(state, { payload: newState }) {
            return { ...state, ...newState };
        },
        copyWidgets(state) {
            let widgetsCopy = [];
            if (state.selectedWidgets.length > 0) {
                widgetsCopy = state.selectedWidgets;
                return { ...state, widgetsCopy };
            }
            return state;
        },
    },
    effects: {
        *pasteWidgets(payload, { select, put }) {
            const editor = yield select(state => state.editor);
            const { widgetsCopy, selectedPageId } = editor;
            // 调取批量新增接口
            if (widgetsCopy.length > 0) {
                const newWidgets = [];
                for (let i = 0; i < widgetsCopy.length; i++) {
                    const newWidget = yield put({
                        type: 'widgets/addWidget',
                        payload: {
                            pageId: selectedPageId,
                            widget: { ...widgetsCopy[i], form_system: true },
                            batch: true,
                        },
                    });
                    newWidgets.push(newWidget);
                }
                yield put({
                    type: 'widgets/selectWidgets',
                    payload: newWidgets,
                });
            }
        },
        *saveEditorType({ payload: editorType }, { put, call }) {
            yield call(TemplatesService.saveEditorType, editorType);
            yield put({
                type: 'setState',
                payload: { editorType },
            });
        },
        *createTemplate({ payload: resourceId }, { put, call }) {
            yield call(TemplatesService.importArResource, resourceId);
            yield put({ type: 'pageJump/fetchPageJump' });
            yield put({ type: 'pages/fetchPages' });
        },
        *keyEvent({ payload }, { select, put }) {
            const editor = yield select(state => state.editor);
            const { selectedWidgets } = editor;
            switch (payload.type) {
            case 'up': {
                const newWidgets = selectedWidgets.map((s) => { s.offset_y += payload.offset; return s; });
                yield put({ type: 'widgets/updateWidgets', payload: newWidgets });
                yield put({ type: 'setState', payload: { selectedWidgets: newWidgets } });
                break;
            }
            case 'down': {
                const newWidgets = selectedWidgets.map((s) => { s.offset_y -= payload.offset; return s; });
                yield put({ type: 'widgets/updateWidgets', payload: newWidgets });
                yield put({ type: 'setState', payload: { selectedWidgets: newWidgets } });
                break;
            }
            case 'left': {
                const newWidgets = selectedWidgets.map((s) => { s.offset_x -= payload.offset; return s; });
                yield put({ type: 'widgets/updateWidgets', payload: newWidgets });
                yield put({ type: 'setState', payload: { selectedWidgets: newWidgets } });
                break;
            }
            case 'right': {
                const newWidgets = selectedWidgets.map((s) => { s.offset_x += payload.offset; return s; });
                yield put({ type: 'widgets/updateWidgets', payload: newWidgets });
                yield put({ type: 'setState', payload: { selectedWidgets: newWidgets } });
                break;
            }
            case 'copy':
                yield put({ type: 'copyWidgets' });
                break;
            case 'paste':
                yield put({ type: 'pasteWidgets' });
                break;
            case 'delete':
                yield put({ type: 'widgets/removeWidgets', payload: selectedWidgets });
                yield put({ type: 'widgets/selectedWidgets', payload: [] });
                break;
            case 'undo':
                break;
            case 'redo':
                break;
            case 'save':
                yield put({ type: 'data/saveEditorData' });
                break;
            default:
                break;
            }
        },
    },
    subscriptions: {
        keyboardWatcher({ dispatch }) {
            key('up', () => { dispatch({ type: 'keyEvent', payload: { type: 'up', offset: 5 } }); });
            key('down', () => { dispatch({ type: 'keyEvent', payload: { type: 'down', offset: 5 } }); });
            key('left', () => { dispatch({ type: 'keyEvent', payload: { type: 'left', offset: 5 } }); });
            key('right', () => { dispatch({ type: 'keyEvent', payload: { type: 'right', offset: 5 } }); });
            key('shift+up', () => { dispatch({ type: 'keyEvent', payload: { type: 'up', offset: 1 } }); });
            key('shift+down', () => { dispatch({ type: 'keyEvent', payload: { type: 'down', offset: 1 } }); });
            key('shift+left', () => { dispatch({ type: 'keyEvent', payload: { type: 'left', offset: 1 } }); });
            key('shift+right', () => { dispatch({ type: 'keyEvent', payload: { type: 'right', offset: 1 } }); });
            key('⌘+c, ctrl+c', () => { dispatch({ type: 'keyEvent', payload: { type: 'copy' } }); });
            key('⌘+v, ctrl+v', () => { dispatch({ type: 'keyEvent', payload: { type: 'paste' } }); });
            key('delete,backspace', () => { dispatch({ type: 'keyEvent', payload: { type: 'delete' } }); });
            key('⌘+z, ctrl+z', () => { dispatch({ type: 'keyEvent', payload: { type: 'undo' } }); });
            key('⌘+shift+z, ctrl+shift+z', () => { dispatch({ type: 'keyEvent', payload: { type: 'redo' } }); });
            key('⌘+s, ctrl+s', () => { dispatch({ type: 'keyEvent', payload: { type: 'save' } }); });
        },
    },
};
